#define COMPILE_MODE_1 compileFinal
#define COMPILE_MODE_2 preprocessFile

//自带组队功能U键使用
["Initialize", [true]] call BIS_fnc_dynamicGroups;


//_____________________________________参数转变量______________________________
[] execVM "functions\fn_parameterInit.sqf";
waitUntil{!isNil "Param_Debug"};

//_____________________________________默认难度覆盖参数______________________________
#include "include\difficulty.hpp"

//特殊值转换
Param_EnemyEscape = Param_EnemyEscape / 100;

//ai倒地自爆几率
as_ai_down = (100 - Param_EnemyEscape) * 0.3;

//版本获取
Param_Version = floor(productVersion select 2);
publicVariable "Param_Version";

//mod rhs
if(Param_RHS == 1
&& {isNull (configFile >> "rhs_side_debug")})then{
	Param_RHS = 0;
	publicVariable "Param_RHS";
};

//mod cup
if(Param_CUP_Weapons == 1
&& {isNull (configFile >> "CUP_AA12AmmoExplosion")})then{
	Param_CUP_Weapons = 0;
	publicVariable "Param_CUP_Weapons";
};

as_param_inited = true;
publicVariable "as_param_inited";

//_____________________________________自定义类型______________________________
#include "Units\commando.sqf"
#include "Units\unit.sqf"
#include "Units\weapon.sqf"
#include "Units\parts.sqf"
#include "Units\headgear.sqf"
#include "Units\support.sqf"
#include "Units\launch.sqf"
#include "Units\talk.sqf"
#include "Units\astox.sqf"
//_____________________________________编译函数______________________________
#include "functions\initFunctions.hpp"
call fn_fnc;

//_____________________________________编译据点______________________________
#include "Units\E\all.sqf"

//_____________________________________AI技能______________________________
#include "include\ai_skill.hpp"

	
//_____________________________________Debug参数______________________________
if(Param_Debug == 1) then
{
	//Param_EnemySpawnNum = 1.0;
	//Param_WeaponNum = 4;
	Param_ShowMarker = 2;
	//Param_EnemyEscape = 95;
	setGroupIconsVisible [true, true];

};


//简单的保存功能
if(Param_Version > 181)then{
	[] spawn fn_easySL;
};

//玩家俘虏击杀数
as_captive_kill_num = 0;

//玩家平民击杀数
as_civ_kill_num = 0;

//蓝军与绿军关系
west setFriend [resistance, Param_BGRelation];
resistance setFriend [west, Param_BGRelation];

//蓝军与平民关系
west setFriend [civilian, 1];
civilian setFriend [west, 1];
//绿军与平民关系
resistance setFriend [civilian, 1];
civilian setFriend [resistance, 1];

//红军与平民关系
east setFriend [civilian, 1];
civilian setFriend [east, 1];

//红军与蓝军关系
east setFriend [west, 0];
west setFriend [east, 0];
//红军与绿军关系
east setFriend [resistance, 0];
resistance setFriend [east, 0];

[] spawn
{
	waitUntil{as_civ_kill_num >= Param_CivNum};
	//east setFriend [civilian, 0]; 这句话执行会导致玩家访问不了箱子
	[civilian, "当地新闻广播", "最近岛上流窜着杀人狂，他们专们打劫平民的财产，请各位居民注意不要轻易出行！"] remoteExec ["fn_talk", 0];
	civilian setFriend [east, 0];
};


as_debug_spawn_dt = 600;//刷兵间隔


//每10分钟就设置一次，防止bug
[]spawn
{
	while{true}do
	{
		if(Param_SimulationDistance < 300)then{Param_SimulationDistance = 1200;};
		
		enableDynamicSimulationSystem true;
		//__动态模拟设置
		"Group" setDynamicSimulationDistance Param_SimulationDistance;
		"Vehicle" setDynamicSimulationDistance Param_SimulationDistance;
		"EmptyVehicle"  setDynamicSimulationDistance (Param_SimulationDistance * 0.5);//空载具
		"Prop" setDynamicSimulationDistance Param_SimulationDistance;//建筑物
		"IsMoving" setDynamicSimulationDistanceCoef 1.25;//1.66666;//移动物体系数
		Sleep 600;
	};
};

//100最多 默认数据，每个岛单独配置再覆盖
as_num_g0 = 75;//突击队数量
as_num_g1 = 70;//普通部队数量
as_num_g2 = 65;//守卫数量

/////////////////////////////////////////////////////////////
//不同岛的参数不一样
if(worldName == "Altis") then
{
	//刷兵数量调整
	Param_EnemySpawnNum = Param_EnemySpawnNum * 0.82;
	//距离判断缩放 
	as_near_scale = 1.1;

	as_map_x = 1900;
	as_map_y = 4700;
	as_map_w = 26500;
	as_map_h = 21145;

	//__营地数量
	as_num_e0 = 8;
	as_num_e1 = 11;
	as_num_e2 = 13;
	as_num_e3 = 18;
	as_num_e4 = 7;
	as_num_e5 = 5;	//指挥部
};

if(worldName == "Tanoa") then
{
	//刷兵数量调整
	Param_EnemySpawnNum = Param_EnemySpawnNum * 0.70;
	//距离判断缩放 
	as_near_scale = 0.8;

	as_map_x = 1000;
	as_map_y = 1000;
	as_map_w = 13000;
	as_map_h = 14000;

	//__营地数量
	as_num_e0 = 6;
	as_num_e1 = 9;
	as_num_e2 = 10;
	as_num_e3 = 13;
	as_num_e4 = 5;
	as_num_e5 = 3;	//指挥部
	
	as_num_g0 = 75;//突击队数量
	as_num_g1 = 65;//普通部队数量
	as_num_g2 = 50;//守卫数量
};

if(worldName == "Malden") then
{
	//刷兵数量调整
	Param_EnemySpawnNum = Param_EnemySpawnNum * 0.65;
	//距离判断缩放 
	as_near_scale = 0.75;

	as_map_x = 200;
	as_map_y = 300;
	as_map_w = 11800;
	as_map_h = 12800;

	//__营地数量
	as_num_e0 = 2;
	as_num_e1 = 7;
	as_num_e2 = 6;
	as_num_e3 = 6;
	as_num_e4 = 3;
	as_num_e5 = 2;	//指挥部
};

if(worldName == "Stratis") then
{
	//刷兵数量调整
	Param_EnemySpawnNum = Param_EnemySpawnNum * 0.5;
	//距离判断缩放 
	as_near_scale = 0.45;

	as_map_x = 1200;
	as_map_y = 300;
	as_map_w = 5500;
	as_map_h = 7300;

	//__营地数量
	as_num_e0 = 1;
	as_num_e1 = 5;
	as_num_e2 = 5;
	as_num_e3 = 5;
	as_num_e4 = 2;
	as_num_e5 = 1;	//指挥部
};

if(worldName == "Enoch") then
{
	//刷兵数量调整
	Param_EnemySpawnNum = Param_EnemySpawnNum * 0.7;
	//距离判断缩放 
	as_near_scale = 0.9;

	as_map_x = 500;
	as_map_y = 500;
	as_map_w = 12000;
	as_map_h = 12000;

	//__营地数量
	as_num_e0 = 2;
	as_num_e1 = 7;
	as_num_e2 = 9;
	as_num_e3 = 8;
	as_num_e4 = 3;
	as_num_e5 = 3;	//指挥部
};
//CUP Mpas
if(worldName == "takistan") then
{
	//刷兵数量调整
	Param_EnemySpawnNum = Param_EnemySpawnNum * 0.89;
	//距离判断缩放 
	as_near_scale = 1.15;

	as_map_x = 400;
	as_map_y = 400;
	as_map_w = 12000;
	as_map_h = 12000;

	//__营地数量
	as_num_e0 = 2;
	as_num_e1 = 8;
	as_num_e2 = 10;
	as_num_e3 = 12;
	as_num_e4 = 4;
	as_num_e5 = 2;	//指挥部
};

if(worldName == "Chernarus_Winter"
||worldName == "chernarus_summer") then
{
	//刷兵数量调整
	Param_EnemySpawnNum = Param_EnemySpawnNum * 0.82;
	//距离判断缩放 
	as_near_scale = 1.0;

	as_map_x = 500;
	as_map_y = 1500;
	as_map_w = 13500;
	as_map_h = 13000;

	//__营地数量
	as_num_e0 = 2;
	as_num_e1 = 6;
	as_num_e2 = 8;
	as_num_e3 = 8;
	as_num_e4 = 5;
	as_num_e5 = 3;	//指挥部
};

if(worldName == "sara") then
{
	//刷兵数量调整
	Param_EnemySpawnNum = Param_EnemySpawnNum * 0.79;
	//距离判断缩放 
	as_near_scale = 1.0;

	as_map_x = 1500;
	as_map_y = 1500;
	as_map_w = 18000;
	as_map_h = 17500;

	//__营地数量
	as_num_e0 = 2;
	as_num_e1 = 8;
	as_num_e2 = 10;
	as_num_e3 = 10;
	as_num_e4 = 6;
	as_num_e5 = 3;	//指挥部
};

if(worldName == "Woodland_ACR") then
{
	//刷兵数量调整
	Param_EnemySpawnNum = Param_EnemySpawnNum * 0.88;
	//距离判断缩放 
	as_near_scale = 0.8;

	as_map_x = 300;
	as_map_y = 300;
	as_map_w = 7200;
	as_map_h = 7200;

	//__营地数量
	as_num_e0 = 1;
	as_num_e1 = 6;
	as_num_e2 = 7;
	as_num_e3 = 8;
	as_num_e4 = 4;
	as_num_e5 = 2;	//指挥部
};

//数量调整
as_num_e0 = ceil(as_num_e0 * Param_E_0);
as_num_e1 = ceil(as_num_e1 * Param_E_1);
as_num_e2 = ceil(as_num_e2 * Param_E_2);
as_num_e3 = ceil(as_num_e3 * Param_E_3);
as_num_e4 = ceil(as_num_e4 * Param_E_4);


as_num_e_num_all = 0;
as_num_e_num_hold = 0;
publicVariable "as_num_e_num_all";
publicVariable "as_num_e_num_hold";

as_npc_dead = false;
as_map_viewed = false;

//听闻枪声ai单位组
as_ai_groups = [];

//平民ai救人组
as_group_medic = grpNull;//创建ai时才创建防止bug

//总计GN AI数量
as_gn_num = 0;
//请求支援位置
as_gn_target = [];

//绘制名字单位
as_units_sn = [];

[]spawn
{
	while{true} do
	{
		sleep 300;
		as_ai_groups = as_ai_groups select {!isNull _x && {count units _x >= 5}};//大于五人才进攻
	};
};



//俘虏组
//as_captive_group = createGroup civilian;//会自动变为 未知阵营
//as_captive_group enableDynamicSimulation true;

 

//时间
[] call fn_time;

//T岛特殊天气
if(worldName == "Tanoa") then
{
	[] spawn
	{
		while{true}do
		{
			Sleep 60;
			0 setFog 0;
		};
	};
	
}
else
{
	//天气
	[] spawn fn_weather;
};

//编辑器放置的单位不清理
{
	_x setVariable["no_cleanup", true];
} forEach ((allMissionObjects "Car") + (allMissionObjects "Tank") + (allMissionObjects "Ship") + (allMissionObjects "Air"));


//_____________________________________精准投掷______________________________
//kk_throw_key = 36;
0 spawn COMPILE_MODE_1 COMPILE_MODE_2 "Scripts\kk_throw.sqf";

//________________________________AL手雷_________________________
// by ALIAS

enable_toxic_grenade		= true; publicVariable "enable_toxic_grenade";
enable_incendiary_grenade	= true; publicVariable "enable_incendiary_grenade";
enable_stun_grenade			= true; publicVariable "enable_stun_grenade";
enable_emp_grenade			= true; publicVariable "enable_emp_grenade";

// damage done on units
tox_damage		= 0.1; publicVariable "tox_damage"; // damage inflicted over time by toxic gas
emp_damage		= 0.1; publicVariable "emp_damage"; // damage on units inflicted by electroshock
incend_damage	= 0.02; publicVariable "incend_damage"; // damage inflicted on players by incendiary grenade over time 

// damage done by fire on buildings
building_dam = 0.5; publicVariable "building_dam";

// protective gear against toxic gas
tox_prot="H_PilotHelmetFighter_B"; publicVariable "tox_prot";

// class name of the animation use by player to estinguish fire
off_fire = ["amovppnemstpsraswrfldnon_amovppnemevaslowwrfldl","amovppnemstpsraswrfldnon_amovppnemevaslowwrfldr","amovppnemstpsnonwnondnon_amovppnemevasnonwnondl","amovppnemstpsnonwnondnon_amovppnemevasnonwnondr","amovppnemstpsraswpstdnon_amovppnemevaslowwpstdl","amovppnemstpsraswpstdnon_amovppnemevaslowwpstdr"];
publicVariable "off_fire";

// turrets that can be affectes by EMP
static_turrets_emp = ["B_static_AA_F", "B_static_AT_F","B_T_Static_AA_F","B_T_Static_AT_F","B_T_GMG_01_F","B_T_HMG_01_F","B_T_Mortar_01_F","B_HMG_01_high_F","B_HMG_01_A_F","B_GMG_01_F","B_GMG_01_high_F","B_GMG_01_A_F","B_Mortar_01_F","B_G_Mortar_01_F","B_Static_Designator_01_F","B_AAA_System_01_F","B_SAM_System_01_F","B_SAM_System_02_F","O_HMG_01_F","O_HMG_01_high_F","O_HMG_01_A_F","O_GMG_01_F","O_GMG_01_high_F","O_GMG_01_A_F","O_Mortar_01_F","O_G_Mortar_01_F","O_static_AA_F","O_static_AT_F","O_Static_Designator_02_F","I_HMG_01_F","I_HMG_01_high_F","I_HMG_01_A_F","I_GMG_01_F","I_GMG_01_high_F","I_GMG_01_A_F","I_Mortar_01_F","I_G_Mortar_01_F","I_static_AA_F","I_static_AT_F"];
publicVariable "static_turrets_emp";

// helmets that can be affectes by EMP
special_helmet_emp = ["H_PilotHelmetFighter_B", "H_PilotHelmetFighter_O","H_HelmetO_ViperSP_ghex_F","H_PilotHelmetFighter_I","H_HelmetO_ViperSP_hex_F"];
publicVariable "special_helmet_emp";

// launchers that can be affectes by EMP
special_launchers_emp = ["launch_RPG32_F","launch_O_Titan_F","launch_O_Titan_short_F","launch_MRAWS_sand_F","launch_NLAW_F","launch_B_Titan_short_F","launch_B_Titan_F","launch_B_Titan_short_tna_F","launch_B_Titan_tna_F","launch_MRAWS_green_F","launch_MRAWS_olive_rail_F","launch_I_Titan_short_F","launch_I_Titan_F","launch_RPG32_ghex_F","launch_O_Vorona_green_F","launch_O_Titan_short_ghex_F","launch_O_Titan_ghex_F","launch_O_Vorona_brown_F"];
publicVariable "special_launchers_emp";

// street lamps affected by EMP or fire
street_lapms = ["Land_fs_roof_F","Land_TTowerBig_2_F","Land_TTowerBig_1_F","Lamps_base_F","PowerLines_base_F","PowerLines_Small_base_F","Land_LampStreet_small_F","Land_fs_sign_F"];
publicVariable "street_lapms";

// vehicles affected by fire or EMP
vik_list = ["CAR","PLANE","HELICOPTER","Motorcycle","Air","Ship","StaticWeapon"];//,"TANK"
publicVariable "vik_list";
// for tanks there is 50% chance to be destroyed by fire, therefore sometimes fire will go off without destroying the tank

// vegetation affected by fire
list_vegetation = ["TREE","SMALL TREE","BUSH","FOREST BORDER","FOREST TRIANGLE","FOREST SQUARE","FOREST"];
publicVariable "list_vegetation";

// buildings which can be affected by fire
buildings_list = ["BUILDING","HOUSE","CHURCH","CHAPEL","FUELSTATION","HOSPITAL","RUIN","BUNKER"];
publicVariable "buildings_list";

// class names for units you have in your mission
list_man = ["Civilian","SoldierGB","SoldierEB","SoldierWB"]; // "CAManBase"
publicVariable "list_man";

/////////////////////// DO NOT EDIT LINES BELOW ---------------------------------------------------------------------------------

[] spawn {
	if (isNil "allPlayers_on") then 
	{
		chk_players = true;
		while {chk_players} do 
		{
			//allPlayers_on = allPlayers_on - entities "HeadlessClient_F";
			allPlayers_on = call BIS_fnc_listPlayers;
			publicVariable "allPlayers_on";
			sleep 60;
		};
	}
};

//初始点
[] spawn fn_initStart;

waitUntil{!isNil "as_start_pos"};
if(Param_Debug == 1) then
{
	systemChat "初始点初始化完成……";
};


waitUntil{!isNull(call fn_getLivePlayer)};
if(Param_Debug == 1) then
{
	systemChat "有红军进入了游戏……";
};


//防止传送bug
[] spawn fn_tpPlayers;

//_____________________________________Debug设置玩家______________________________
if(Param_Debug == 1) then
{	//
	{  
		_x allowDamage false; 
		_x setCaptive true;  

		/*_x spawn{
			while{true} do
			{
				_this setDamage 0.51;
			};
		};
		*/
		  
		_x addItem "ItemMap";  
		_x assignItem "ItemMap";  
		_x addItem "ItemRadio";  
		_x assignItem "ItemRadio";  
		[_x, "SupportM48"] call BIS_fnc_addCommMenuItem;  
		[_x, "SupportD41"] call BIS_fnc_addCommMenuItem;  
		[_x, "SupportTank"] call BIS_fnc_addCommMenuItem;  
	}forEach AllPlayers;  
	skipTime 3;
	//as_map_viewed = true;//as_npc_dead = true;
	//[] call fn_cleanup_all;
	
	
	as_waypoint_cur = 0;
};

//等待FFIS系统完成初始化
waitUntil{!isNil "ss_FFIS_Ready"};

//下车组
as_group_g = createGroup resistance;
as_group_g enableDynamicSimulation true;
as_group_g spawn fn_squadAI;

//红军遣散组
as_group_r = createGroup east;
as_group_r enableDynamicSimulation true;


//平民车
[] spawn fn_initCivCar;

//刷兵
[] spawn fn_spawnAI;

//建筑守卫
[] spawn fn_buildingGuard;



//巡逻队 暂时取消
//[] spawn fn_initPatrol;



//不动惩罚
[] spawn fn_spawnS;

as_server_done = true;
publicVariable "as_server_done";

//任务点（包含E5指挥部）
[] call fn_initENew;

//事故点（现在改名叫ES）
[] spawn fn_spawnA;


if(Param_Debug == 1) then
{
	systemChat "所有据点初始化完成……";
};

//额外武直点
[] spawn fn_createAir;

//玩家额外逻辑
call fn_updatePlayers;

//玩家队伍
as_group_p = group (call fn_getPlayerRed select 0);

//删除玩家所有路点
//private _group_p = group (AllPlayers select 0);
//while {(count (waypoints _group_p)) > 0} do
//{
//	deleteWaypoint ((waypoints _group_p) select 0);
//};

//





//-----------------------------------FAR_End-----------------------------------
//放到外面，防止不能成功完成任务
as_game_over = false;
as_game_restart = false;
if(Param_GameOver == 1) then
{
	[] spawn 
	{
		
		
		waitUntil
		{
			sleep 5;
			count (call fn_getPlayerRed) > 3
			&&
			{
				_x getVariable ['FAR_isUnconscious',0] == 1
			} count (call fn_getPlayerRed) == count (call fn_getPlayerRed)
		};
		
		as_game_over = true;

		if(as_npc_dead) then
		{
			//所有人倒地，但上校已经死了
			[east, "STR_TAG_O_15_0", "STR_TAG_O_15_1"] remoteExec ["fn_talk", east];
			[resistance, "阻止了敌人撤离", "虽然Garvas上校牺牲了，但敌人也别想活下来！"] remoteExec ["fn_talk", resistance];
		}
		else
		{
			if(!isNil "as_end_pos") then
			{
				//所有人倒地，查看了指挥部
				[east, "STR_TAG_O_16_0", "STR_TAG_O_16_1"] remoteExec ["fn_talk", east];
				[resistance, "消灭了红军小队", "你作为一名勇敢的战士，成功的守护了伟大的领袖Garvas上校，敌人的诡计没有得逞！"] remoteExec ["fn_talk", resistance];
			}
			else
			{
				//所有人倒地，但没有查看指挥部
				[east, "STR_TAG_O_16_0", "STR_TAG_O_17_1"] remoteExec ["fn_talk", east];
				[resistance, "消灭了红军小队", "你作为一名勇敢的战士，成功的守护了伟大的领袖Garvas上校，敌人的诡计没有得逞！"] remoteExec ["fn_talk", resistance];
			}
			
		};
		as_game_restart = true;
	};
};

//-----------------------------------Escape-----------------------------------
[] spawn 
{	
	//等到npc被发现
	waitUntil{as_map_viewed};

	
	[]spawn fn_createEND;
	
	//等到npc死
	waitUntil{sleep 2;as_npc_dead};
	
	//"Dead" remoteExec ["playMusic"];
	
	//游戏还未结束
	if(as_game_over)exitWith{};
	
	[]spawn fn_createEscape;
};


[] spawn 
{
	waitUntil{Sleep 5;as_game_restart};
	uiSleep 20;
	"End13" call BIS_fnc_endMissionServer;
};

//宙斯添加所有对象
if(Param_Debug == 1) then
{	
	[]spawn
	{
		while{true} do
		{
			sleep 30;
			{
				_x addCuratorEditableObjects [vehicles,true ];
			}forEach allCurators;
		};
	};
};

